package com.wty.mianshi.tree;


/**
 * 循环遍历树
 */
public class TreeIterator {

    //先序遍历
    public void preOrder(TreeNode root){
        if(root==null){
            return;
        }
        System.out.print(root.getValue());
        preOrder(root.getLeft());
        preOrder(root.getRight());
    }


    public void inOrder(TreeNode root){
        if(root==null){
            return;
        }
        inOrder(root.getLeft());
        System.out.print(root.getValue());
        inOrder(root.getRight());
    }

    public void postOrder(TreeNode root){
        if(root==null){
            return;
        }
        postOrder(root.getLeft());
        postOrder(root.getRight());
        System.out.print(root.getValue());
    }











    public static void main(String args[]){
        TreeCreator treeCreator=new TreeCreator();
        TreeNode root=treeCreator.createSampleTree();
        TreeIterator treeIterator=new TreeIterator();

        System.out.print("先序遍历"+" ");
        treeIterator.preOrder(root);
        System.out.println();


        System.out.print("中序遍历"+" ");
        treeIterator.inOrder(root);
        System.out.println();



        System.out.print("右序遍历"+" ");
        treeIterator.postOrder(root);
        System.out.println();
    }
}
